package com.plian.system.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.system.entity.sys.Message;
import com.plian.system.entity.sys.SystemRecord;
import com.plian.system.enumeratea.FormTableEnum;
import com.plian.system.service.cqzc.bondissue.BondsIssueService;
import com.plian.system.service.fr.indicators.IComplexIndicatorsValueService;
import com.plian.system.service.fr.indicatorsstdvalue.IndicatorsStdvalueService;
import com.plian.system.service.pf.IBaseGrantSalaryService;
import com.plian.system.service.sys.message.IMessageService;
import com.plian.system.service.sys.system.ISystemRecordService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2024/8/12 2:02 PM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Component
@Slf4j
public class XxlJobHandler {

    @Autowired
    private BondsIssueService bondissueService;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IBaseGrantSalaryService baseGrantSalaryService;
    @Autowired
    private ISystemRecordService systemRecordService;
    @Autowired
    IndicatorsStdvalueService indicatorsStdvalueService;
    @Autowired
    private  IComplexIndicatorsValueService complexIndicatorsService;

//    @Scheduled(cron = "0 0 0 * * ? ")
    @XxlJob("updateStatus")
    public void updateStatus(){
        log.info("直接监管企业 净资产收益率、归母净资产收益率 生成 ------------>" + LocalDateTime.now());
        Calendar cal = Calendar.getInstance();
        Date date=new Date();//现在的日期
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int startMonth = month - 3 > 0 ? month - 3 : 1;
        for (int i = startMonth; i <= month; i++) {
            complexIndicatorsService.updateByYearAndMonth(year, i);
        }
        log.info("直接监管企业 净资产收益率、归母净资产收益率 结束 ------------> " + LocalDateTime.now());
    }

    //    @Scheduled(cron = "0 0 2 * * ?")
    @XxlJob("zhibiaoTask")
    private void zhibiaoTask() {
        indicatorsStdvalueService.calculation();
    }

    //债券发行  到期消息生成
    @XxlJob("bondTasks")
//    @Scheduled(cron = "0 * * * * ?")
    private void bondTasks() {
        List<Message> messages = bondissueService.noRepaymentList();
        messageService.remove(new QueryWrapper<Message>()
                .eq("form_type", "" + FormTableEnum.CQZC_BONDISSUE.getFormType()));
        if (CollectionUtil.isNotEmpty(messages)) {
            messageService.saveBatch(messages);
        }
    }

    //工资发放管理 超过预警线信息生成
    @XxlJob("salaryTasks")
//    @Scheduled(cron = "0 0 1 * * ?")
    private void salaryTasks() {
        List<Message> messages = baseGrantSalaryService.getWarningMessage();
        messageService.remove(new QueryWrapper<Message>()
                .eq("form_type", "salaryWarning"));
        if (CollectionUtil.isNotEmpty(messages)) {
            messageService.saveBatch(messages);
        }
    }

    //制度上报失效
    @XxlJob("configureTasks")
//    @Scheduled(cron = "0 0 1 * * ?")
    private void configureTasks() {
        log.info("制度上报失效 开始--------------> " + LocalDateTime.now());
        List<SystemRecord> systemRecords = systemRecordService.list(new QueryWrapper<SystemRecord>()
                .eq("system_status", 1).isNotNull("failure_time"));
        if (CollectionUtil.isNotEmpty(systemRecords)) {
            long now = DateUtil.parse(DateUtil.format(DateUtil.now(), DateUtil.PATTERN_DATE), DateUtil.PATTERN_DATE).getTime();
            List<SystemRecord> updateList = new ArrayList<>();
            for (SystemRecord systemRecord : systemRecords) {
                if (systemRecord.getFailureTime().getTime() <= now) {
                    systemRecord.setSystemStatus(2);
                    updateList.add(systemRecord);
                    log.info("制度上报失效 --------------> 制度名称：" + systemRecord.getSystemName() + LocalDateTime.now());
                }
            }
            if (CollectionUtil.isNotEmpty(updateList)) {
                systemRecordService.updateBatchById(updateList);
            }
        }

        log.info("制度上报失效 结束--------------> " + LocalDateTime.now());
    }


}
